/**
 * Created by admin on 2017/10/30.
 */
/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function() {
    $("form").submit(function(){
        return false;
    });
    var VAL ={
        NORMAL       : "0000000"  ,
        EVT_USER     : $("#userId").text(),
        T_FBPBISOPE	 : 'FBPBISOPE',
        T_FBPBISTOL  : 'FBPBISTOL',
        T_FBPMDLDEF  : "FBPMDLDEF",
        T_FPPMOVEIN  : 'FPPMOVEIN',
        T_FPPMVINCCL : 'FPPMVINCCL',
        T_FPPPACKCLM : "FPPPACKCLM",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };
    var domObj = {
        buttons : {
            $query_btn: $("#query_btn"),
            $save_btn : $("#save_btn"),
            $clean_btn: $("#clean_btn"),
        $clean_box_btn: $("#clean_box_btn"),
        },
        /*$factorySite    : $("#factorySite"),*/
        $bayId          : $("#bayId"),
      /*  $factoryId      : $("#factoryId"),*/
        $woIdFk         : $("#woIdFk"),
        $toolIdFk       : $("#toolIdFk"),
        $prdSeqId       : $("#prdSeqId"),

        $prdouctId      : $("#prdouctId"),
        $mdlIdFk        : $("#mdlIdFk"),
        $prdGrade       : $("#prdGrade"),
        $lotId          : $("#lotId"),
        grids : {
            $boxListDiv    : $("#boxListDiv"),
            $boxListGrd    : $("#boxListGrd"),
            $boxListPg     : $("#boxListPg"),
        },
        dialogs : {
            $prd_sureBtn   : $("#prd_sureBtn"),
            $packDialog    : $("#packDialog"),
            $prd_seq_id    : $("#prd_seq_id"),

            $productDialog     : $("#productDialog"),
            $prduct_sureBtn    : $("#prduct_sureBtn"),

            $productListDiv    : $("#productListDiv"),
            $productListGrd    : $("#productListGrd"),
            $productListPg     : $("#productListPg"),


            $packdelDialog     : $("#packdelDialog"),
            $prd_id_fk         : $("#prd_id_fk"),
            $prdSureBtn        : $("#prdSureBtn"),

            $WeightDialog      : $("#WeightDialog"),
            $nWeight            : $("#nWeight"),
            $gWeight           : $("#gWeight"),
            $weightSureBtn     : $("#weightSureBtn"),
            $weightCancelBtn   : $("#weightCancelBtn"),


        },
    }
    var print;//打印数据全局变量
    function initInner() {
        var boxListCM = [
            {name: 'modifyItem', index: 'modifyItem',   label: DETAIL_TAG,     sortable: false  ,width: 50 },
            {name: 'prd_grade',  index: 'prd_grade',   	label: PRD_GRADE_TAG,   width: 60},
            {name: 'prd_qty',    index: 'prd_qty',   	label: PRD_QTY_TAG,   width: 60},
            {name: 'box_id_fk',  index: 'bo_id_fk',   	label: BOX_ID_TAG,   width: 130},
            {name: 'mdl_id_fk',  index: 'mdl_id_fk',   	label: PRODUCT_ID_TAG,   width: 100},
            {name: 'box_cnt',    index: 'box_cnt',   	label: BOX_MAX_CNT_TAG ,   width: 100},
            {name: 'wo_id_fk',   index: 'wo_id_fk',   	label: WO_ID_TAG,   width: 100},
        ];
        domObj.grids.$boxListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:510,
            width:domObj.grids.$boxListDiv.width(),
            autowidth : false,
            shrinkToFit : false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
            multiselect : false,
            pager : domObj.$boxListPg,
            colModel: boxListCM,
            gridComplete:function(){
             var ids = domObj.grids.$boxListGrd.jqGrid('getDataIDs');
               for(var i=0; i<ids.length;i++){
                   var id=ids[i];
                   var modify = "<button id=" + "'mod" + id + "'" + " style='color:#ff6600'>明细</button>";
                   domObj.grids.$boxListGrd.jqGrid('setRowData', id, { modifyItem: modify});
                   $("#mod" + id).click(toolFunc.modify_func );
               }
             },
        });
        var productListCM = [
            {name: 'prd_seq_id', index: 'prd_seq_id',   label: SHEET_ID_TAG,    width: 130},
            {name: 'mdl_id_fk',  index: 'mdl_id_fk',   	label: MDL_ID_TAG,      width: 130},
            {name: 'prd_grade',  index: 'prd_grade',   	label: PRD_GRADE_TAG,   width: 130},

        ];
        domObj.dialogs.$productListGrd.jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            height:350,
            width:430,
            autowidth : false,
            shrinkToFit : false,
            resizable : true,
            loadonce : true,
            viewrecords : true,
            rownumbers : true,
            rowNum : 20,
            emptyrecords : true,
            multiselect :false ,
            pager : domObj.dialogs.$productListPg,
            colModel: productListCM,
        });
    }
    var initSelectFnc = {
        iniToolIdSel:function(){
            var inObj = {
                trx_id : VAL.T_FBPBISTOL,
                action_flg : "Q",
                iary : [{
                    tool_group_id_fk  :'$MPK'
                }]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$toolIdFk, outObj.oary, "tool_id","tool_id","",true);
            }
        },
    }
    var toolFunc = {
        modify_func: function() {
            domObj.dialogs.$productDialog.modal({
                backdrop: true,
                keyboard: false,
                show: false
            });
            domObj.dialogs.$productDialog.modal("show");
            var rowData  = domObj.grids.$boxListGrd.jqGrid("getRowData",this.id.substr(3));
            iary = {
                box_id_fk    :  rowData.box_id_fk
            }
            inObj = {
                trx_id : VAL.T_FPPPACKCLM,
                action_flg   : 'N',
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
               setGridInfo(outObj.oary, "#productListGrd");
            }
        },
        query_func : function() {

            var  toolIdFk,inObj,outObj,iary;
            toolIdFk = domObj.$toolIdFk.val().trim();
            if(!toolIdFk){
                showErrorDialog("", "设备代码不能为空");
                return false;
            }
            iary = {
                tool_id_fk    :  toolIdFk
            }
            inObj = {
                trx_id : VAL.T_FPPPACKCLM,
                action_flg   : 'S',
                iary : [iary] ,
                evt_usr : VAL.EVT_USER
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, "#boxListGrd");
            }
        },
        save_func : function() {
            var id = domObj.grids.$boxListGrd.jqGrid('getGridParam','selrow');
            if(id == null){
                showErrorDialog("", "选择一个箱子记录进行包装出账操作");
                return false;
            }
            var NWeight,GWeight;
            NWeight = domObj.dialogs.$nWeight.val();
            GWeight = domObj.dialogs.$gWeight.val();

            var iary =[];
            for(var i=0;i<1;i++){
                var rowData = domObj.grids.$boxListGrd.jqGrid("getRowData",id);
                var iarys={
                  box_id_fk : rowData.box_id_fk
                }
                iary.push(iarys);
            }
            inObj = {
                trx_id : VAL.T_FPPPACKCLM,
                action_flg   : 'A',
                iary : iary ,
                evt_usr : VAL.EVT_USER
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                print = outObj.oary[0];
                for(var i=0;i<1;i++){
                    domObj.grids.$boxListGrd.jqGrid("delRowData",id);
                }
                if(print.wo_id_fk.length==8){
                   var woIdFk = print.wo_id_fk;
                    print.wo_id_fk = woIdFk.slice(0,1)+ woIdFk.slice(2,8)
                }
                printBtnfunc(print.mdl_id_fk,print.mdl_desc,print.wo_id_fk,
                    print.prd_qty,NWeight,GWeight,print.box_id_fk);
                showSuccessDialog("包装出账成功");
            }
        },
        cancel_func : function() {
            domObj.dialogs.$prd_id_fk.val("");
            domObj.dialogs.$packdelDialog.modal({
                backdrop: true,
                keyboard: false,
                show: false
            });
            domObj.dialogs.$packdelDialog.modal("show");
        },
        delPrd_func: function() {
            var prdSeqId = domObj.dialogs.$prd_id_fk.val();
            var iary,inObj,outObj;
            iary = {
                prd_seq_id  :  prdSeqId,
            }
            inObj = {
                trx_id : VAL.T_FPPPACKCLM,
                action_flg   : 'D',
                iary : [iary]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                //删除ret_prd_pack信息后取消入账
                var iaryA = new Array();
                iaryA.push({
                    prd_seq_id:prdSeqId,
                });
                var lengthA = iaryA.length;
                var inTrxObj ={
                    trx_id     : VAL.T_FPPMVINCCL,
                    action_flg : 'C'        ,
                    sht_ope_msg: '',
                    evt_usr	   : VAL.EVT_USER,
                    prd_qty    : lengthA,
                    iary	   : iaryA,
                    opi_ope_flg: "Y",
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    toolFunc.query_func();
                }
                domObj.dialogs.$prd_id_fk.val("");
                showSuccessDialog("包装取消成功");
            }
        },
        delBox_func: function() {
            var id = domObj.grids.$boxListGrd.jqGrid('getGridParam','selrow');
            if(id == null){
                showErrorDialog("", "选择一个箱子进行取消包装操作");
                return false;
            }
            var iary =[];
            var rowData = domObj.grids.$boxListGrd.jqGrid("getRowData",id);
            var iarys={
                box_id_fk : rowData.box_id_fk
            }
            iary.push(iarys);
            var iary,inObj,outObj;
            inObj = {
                trx_id : VAL.T_FPPPACKCLM,
                action_flg   : 'F',
                iary : iary
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                //删除ret_prd_pack信息后取消入账
                 var iaryA = new Array();
                 for(var i=0;i<outObj.oary.length;i++){
                     iaryA.push({
                         prd_seq_id:outObj.oary[i].prd_seq_id,
                     });
                 }
                var lengthA = iaryA.length;
                var inTrxObj ={
                    trx_id     : VAL.T_FPPMVINCCL,
                    action_flg : 'C'        ,
                    sht_ope_msg: '',
                    evt_usr	   : VAL.EVT_USER,
                    prd_qty    : lengthA,
                    iary	   : iaryA,
                    opi_ope_flg: "Y",
                };
                var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
                if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                    domObj.grids.$boxListGrd.jqGrid("delRowData",id);
                }
                showSuccessDialog("包装取消成功");
            }
        }
    }

    var initializationFunc = function(){
        initInner();
        initSelectFnc.iniToolIdSel();
        domObj.dialogs.$prd_id_fk.bind('keyup',function(event){
                if(event.keyCode==13) {
                  toolFunc.delPrd_func();
                }
        }),
        domObj.$prdSeqId.bind('keyup',function(event){
            if(event.keyCode==13){
                //先去查询玻璃是否在当前站点和当前设备，符合则返回等级
                //去包装箱列表遍历，是否存在等级，若存在，则箱子数量加1,玻璃信息存入临时表，
                //不存在则生成一个箱子，在页面显示，信息存入临时表
                var  prdSeqId,toolIdFk,factoryId,bayId,woIdFk,inObj,outObj,iary;
                prdSeqId = $.trim(domObj.$prdSeqId.val());
                toolIdFk = $.trim(domObj.$toolIdFk.val());
               /* factoryId= domObj.$factoryId.val().trim();*/
                bayId    = $.trim(domObj.$bayId.val());
                woIdFk   = $.trim(domObj.$woIdFk.val());
                if(!toolIdFk){
                    showErrorDialog("", "设备代码不能为空");
                    return false;
                }
                if(!prdSeqId){
                    showErrorDialog("", "请输入玻璃代码");
                    return false;
                }
                iary = {
                    prd_seq_id 	 :  prdSeqId,
                    tool_id_fk   :  toolIdFk,
                    wo_id_fk     :  woIdFk
                }
                var inObj1 = {
                    trx_id : VAL.T_FPPPACKCLM,
                    action_flg   : 'C',
                    iary : [iary]
                };
                var outObj1 = comTrxSubSendPostJson(inObj1);
                if (outObj1.rtn_code == VAL.NORMAL) {
                    var ids = domObj.grids.$boxListGrd.jqGrid('getDataIDs');
                        var existFlag ='false';
                        for(var i=0; i==0 || i<ids.length;i++){
                            var data = outObj1.oary[0];
                            var rowData = domObj.grids.$boxListGrd.jqGrid("getRowData",ids[i]);
                            if(data.prd_grade == rowData.prd_grade && data.wo_id_fk == rowData.wo_id_fk){
                                existFlag='true';
                                var prdQty = parseInt(rowData.prd_qty)+1;
                                if(prdQty>data.box_cnt){
                                    showErrorDialog("", "该箱子已经到达最大装载数量");
                                    return false;
                                }
                                rowData.prd_qty = prdQty;
                                domObj.grids.$boxListGrd.jqGrid('setRowData', ids[i], rowData);
                                $("#mod" + ids[i]).click(toolFunc.modify_func);
                                //先判断有没有入账，加入入账操作
                                 if(data.prd_stat=="WAIT"){
                                     iary = {
                                         prd_seq_id : prdSeqId,
                                         wo_id_fk   : data.wo_id_fk,
                                         ope_id_fk  : data.nx_ope_id_fk,
                                         tool_id_fk : toolIdFk
                                     }
                                     var inObj2 = {
                                         trx_id     : VAL.T_FPPMOVEIN,
                                         evt_usr    : VAL.EVT_USER,
                                         action_flg  : 'A',
                                         iary       : [iary]
                                     };
                                     var outObj2 = comTrxSubSendPostJson(inObj2);
                                     if (outObj2.rtn_code != VAL.NORMAL) {
                                          return false;
                                     }
                                 }
                                //信息存入临时表
                                iary = {
                                    prd_seq_id 	 :  data.prd_seq_id,
                                    box_id_fk    :  rowData.box_id_fk
                                }
                               var  inObj3 = {
                                    trx_id : VAL.T_FPPPACKCLM,
                                    action_flg   : 'M',
                                    evt_usr      : VAL.EVT_USER,
                                    iary : [iary]
                                };
                                var outObj3 = comTrxSubSendPostJson(inObj3);
                                if (outObj3.rtn_code == VAL.NORMAL) {
                                   //显示玻璃信息
                                    domObj.$prdouctId.text(data.prd_seq_id);
                                    domObj.$mdlIdFk.text(data.mdl_id_fk);
                                    domObj.$prdGrade.text(data.prd_grade);
                                    domObj.$lotId.text(data.lot_id);
                                    domObj.$bayId.text(data.bay_id);
                                    domObj.$woIdFk.text(data.wo_id_fk);
                                    domObj.$prdSeqId.val("");
                                }
                            }
                        }
                        if(existFlag=='false'){
                            //grid没有当前玻璃的等级直接添加一条数据
                            var data = outObj1.oary[0];
                            //先判断有没有入账，加入入账操作
                            if(data.prd_stat=="WAIT"){
                                iary = {
                                    prd_seq_id : prdSeqId,
                                    wo_id_fk   : data.wo_id_fk,
                                    ope_id_fk  : data.nx_ope_id_fk,
                                    tool_id_fk : toolIdFk
                                }
                                var inObj4 = {
                                    trx_id     : VAL.T_FPPMOVEIN,
                                    evt_usr    : VAL.EVT_USER,
                                    action_flg  : 'A',
                                    iary       : [iary]
                                };
                                var outObj4 = comTrxSubSendPostJson(inObj4);
                                if (outObj4.rtn_code == VAL.NORMAL) {
                                }
                            }
                            if(data.box_flag){
                                showErrorDialog("", "该等级产品已经开始包装，请点击载入包装明细操作");
                                return false;
                            }
                            data.prd_qty = 1;
                            iary = {
                                prd_seq_id : prdSeqId
                            }
                            var inObj5 = {//生成一个箱号
                                trx_id: VAL.T_FPPPACKCLM,
                                action_flg: 'G',
                                evt_usr    : VAL.EVT_USER,
                                iary    : [iary]
                            };
                            var outObj5 = comTrxSubSendPostJson(inObj5);
                            if (outObj5.rtn_code == VAL.NORMAL) {
                                data.box_id_fk = outObj5.oary[0].box_id_fk;
                            }
                            if(!data.box_id_fk){
                                return false;
                            }
                            var newRowId = getGridNewRowID("#boxListGrd");
                            domObj.grids.$boxListGrd.jqGrid("addRowData", newRowId, data);
                            var modify = "<button id=" + "'mod" + newRowId + "'" + " style='color:#f60'>明细</button>";
                            domObj.grids.$boxListGrd.jqGrid('setRowData', newRowId, {modifyItem: modify});
                            $("#mod" + newRowId).click(toolFunc.modify_func);
                            //信息存入临时表
                            iary = {
                                box_id_fk 	 :  data.box_id_fk,
                                prd_seq_id   :  data.prd_seq_id
                            }
                            var inObj6 = {
                                trx_id : VAL.T_FPPPACKCLM,
                                action_flg   : 'M',
                                evt_usr      : VAL.EVT_USER,
                                iary : [iary]
                            };
                            var outObj6 = comTrxSubSendPostJson(inObj6);
                            if (outObj6.rtn_code == VAL.NORMAL) {
                                //显示玻璃信息
                                domObj.$prdouctId.text(data.prd_seq_id);
                                domObj.$mdlIdFk.text(data.mdl_id_fk);
                                domObj.$prdGrade.text(data.prd_grade);
                                domObj.$lotId.text(data.lot_id);
                                domObj.$bayId.text(data.bay_id);
                                domObj.$woIdFk.text(data.wo_id_fk);
                                domObj.$prdSeqId.val("");
                            }
                        }
                   }
               }
        });
    };
    initializationFunc();
         domObj.buttons.$query_btn.click(toolFunc.query_func);
         domObj.buttons.$save_btn.click(toolFunc.save_func);
         domObj.buttons.$clean_btn.click(toolFunc.cancel_func);
         domObj.buttons.$clean_box_btn.click(toolFunc.delBox_func);
         domObj.dialogs.$prdSureBtn.click(toolFunc.delPrd_func);
    //表格自适应
   function resizeFnc(){
    var offsetBottom, grdDivWidth;
    grdDivWidth = $("#divTop").width();
    offsetBottom = $(window).height() - $("#boxListDiv").offset().top;

    $("#boxListDiv").width(grdDivWidth*0.98);
    $("#boxListDiv").height(offsetBottom*0.99);
    $("#boxListDiv").setGridWidth(grdDivWidth*0.97);
    $("#boxListDiv").setGridHeight(offsetBottom*0.95-51);
   };

    resizeFnc();
    $(window).resize(function() {
    resizeFnc();
   });
});
